/**
 * 初始index页面
 * */
//初始化左侧
function InitLeftMenu(_menus) {
    $("#nav").accordion({animate: false});
    //清空之前的菜单栏
    var panels = $("#nav").accordion('panels');
    if (panels) {
        var len = panels.length;
        for (var i = 0; i < len; i++) {
            $("#nav").accordion('remove', 0);
        }
    }
    if (_menus.children) {
        //重点
        $.each(_menus.children, function (i, n) {
            var menulist = '';
            if (n.children) {
                menulist += '<ul>';
                $.each(n.children, function (j, o) {
                    menulist += '<li><div><a ref="' + o.id + '" href="#" rel="' + o.url + '" ><span class="icon icon-a" >&nbsp;</span><span class="nav">' + o.name + '</span></a></div></li> ';
                });
                menulist += '</ul>';
            }
            else {
                //没有具体的子节点，就将自己当做子节点来使用！！！！
                menulist = '<ul></ul>'
            }
            $('#nav').accordion('add', {
                title: n.name,
                iconCls: 'icon icon-b',
                content: menulist
            });
        });
    }
    $('.easyui-accordion li a').click(function () {
        var tabTitle = $(this).children('.nav').text();
        var url = $(this).attr("rel");
        var id = $(this).attr("ref");
        addTab(tabTitle, url);
        $('.easyui-accordion li div').removeClass("selected");
        $(this).parent().addClass("selected");
    }).hover(function () {
        $(this).parent().addClass("hover");
    }, function () {
        $(this).parent().removeClass("hover");
    });
    //选中第一个
    var panels = $('#nav').accordion('panels');
    var t = panels[0].panel('options').title;

}

//获取左侧导航的图标
function getIcon(_menus, menuid) {
    var icon = 'icon ';
    $.each(_menus.children, function (i, n) {
        $.each(n.children, function (j, o) {
            if (o.id == menuid) {
                icon += o.icon;
            }
        })
    });
    return icon;
}

function addTab(subtitle, url, icon) {
    if (!$('#tabs').tabs('exists', subtitle)) {
        $('#tabs').tabs('add', {
            title: subtitle,
            content: createFrame(url),
            //icon:icon,
            closable: true
        });
    } else {
        $('#tabs').tabs('select', subtitle);
        //默认不更新
        //$('#mm-tabupdate').click();
    }
    tabClose();
}

//允许动态更新 add by 1017-05-11
function addTabUpdate(subtitle, url) {
    if ($('#tabs').tabs('exists', subtitle)) {
        //  选择已经存在的
        $('#tabs').tabs('select', subtitle);
        var currTab = $('#tabs').tabs('getSelected');
        if (url != null && url != '') {
            $('#tabs').tabs('update', {
                tab: currTab,
                options: {
                    content: createFrame(url)
                }
            })
        }
    } else {
        $('#tabs').tabs('add', {
            title: subtitle,
            content: createFrame(url),
            closable: true
        });
    }
    tabClose();
}

function createFrame(url) {
    var s = '<iframe scrolling="auto" frameborder="0"  src="' + url + '" style="width:100%;height:100%;"></iframe>';
    return s;
}

function tabClose() {
    /*双击关闭TAB选项卡*/
    $(".tabs-inner").dblclick(function () {
        var subtitle = $(this).children(".tabs-closable").text();
        $('#tabs').tabs('close', subtitle);
    });
    /*为选项卡绑定右键*/
    $(".tabs-inner").bind('contextmenu', function (e) {
        $('#mm').menu('show', {
            left: e.pageX,
            top: e.pageY
        });

        var subtitle = $(this).children(".tabs-closable").text();

        $('#mm').data("currtab", subtitle);
        $('#tabs').tabs('select', subtitle);
        return false;
    });
}

//绑定右键菜单事件
function tabCloseEven() {
    //刷新
    $('#mm-tabupdate').click(function () {
        var currTab = $('#tabs').tabs('getSelected');
        var url = $(currTab.panel('options').content).attr('src');
        if (url != null && url != '') {
            $('#tabs').tabs('update', {
                tab: currTab,
                options: {
                    content: createFrame(url)
                }
            })
        }
    });
    //关闭当前
    $('#mm-tabclose').click(function () {
        var currtab_title = $('#mm').data("currtab");
        $('#tabs').tabs('close', currtab_title);
    });
    //全部关闭
    $('#mm-tabcloseall').click(function () {
        $('.tabs-inner span').not($('.tabs-inner span:first')).each(function (i, n) {
            var t = $(n).text();
            $('#tabs').tabs('close', t);
        });
    });
    //关闭除当前之外的TAB
    $('#mm-tabcloseother').click(function () {
        $('#mm-tabcloseright').click();
        $('#mm-tabcloseleft').click();
    });
    //关闭当前右侧的TAB
    $('#mm-tabcloseright').click(function () {
        var nextall = $('.tabs-selected').nextAll();
        if (nextall.length == 0) {
            return false;
        }
        nextall.each(function (i, n) {
            var t = $('a:eq(0) span', $(n)).text();
            $('#tabs').tabs('close', t);
        });
        return false;
    });
    //关闭当前左侧的TAB
    $('#mm-tabcloseleft').click(function () {
        var prevall = $('.tabs-selected').prevAll();
        if (prevall.length == 1) {
            return false;
        }
        prevall.not(prevall.last()).each(function (i, n) {
            var t = $('a:eq(0) span', $(n)).text();
            $('#tabs').tabs('close', t);
        });
        return false;
    });

    //退出
    $("#mm-exit").click(function () {
        $('#mm').menu('hide');
    })
}

//弹出信息窗口 title:标题 msgString:提示信息 msgType:信息类型 [error,info,question,warning]
function msgShow(title, msgString, msgType) {
    $.messager.alert(title, msgString, msgType);
}

/**
 * 树结构转化
 * @param sNodes
 * @param setting
 * @returns {Array}
 */
function transformTozTreeFormat(sNodes, setting) {

    if (!setting) {
        setting = {idKey: 'id', parentKey: 'pid', childKey: 'children'};
    }
    var i, l,
        key = setting.idKey,
        parentKey = setting.parentKey,
        childKey = setting.childKey;
    if (!key || key == "" || !sNodes) return [];

    var r = [];
    var tmpMap = {};
    for (i = 0, l = sNodes.length; i < l; i++) {
        tmpMap[sNodes[i][key]] = sNodes[i];
    }
    for (i = 0, l = sNodes.length; i < l; i++) {
        if (tmpMap[sNodes[i][parentKey]] && sNodes[i][key] != sNodes[i][parentKey]) {
            if (!tmpMap[sNodes[i][parentKey]][childKey])
                tmpMap[sNodes[i][parentKey]][childKey] = [];
            tmpMap[sNodes[i][parentKey]][childKey].push(sNodes[i]);
        } else {
            r.push(sNodes[i]);
        }
    }
    return r;
}


$(function () {
    var isShow = true;
    //系统时间显示
    setInterval("document.getElementById('nowTime').innerHTML=new Date().toLocaleString()+' 星期'+'日一二三四五六'.charAt(new Date().getDay());", 1000);
    //设置隐藏导航栏功能
    $("#hideHeadBtn").click(function () {
        if (isShow) {
            $("body").layout('collapse', 'north');
            $("#hideHeadBtn").find(".l-btn-text").text("显示导航");
        } else {
            $("body").layout('expand', 'north');
            $("#hideHeadBtn").find(".l-btn-text").text("隐藏导航");
        }
        isShow = !isShow;
    });

    //切换导航栏的class样式
    $('.main-nav').on('click', 'li', function (i, n) {
        $(this).siblings().removeClass('current').end().addClass('current');
    });

    //tab选项卡设置
    tabClose();
    tabCloseEven();


});

